import torch
import torch.nn as nn

# 定义输入数据
X = torch.tensor([-1.0, 0.0, 1.0, 2.0, 3.0, 4.0], dtype=torch.float32).unsqueeze(1)
y = torch.tensor([-3.0, -1.0, 1.0, 3.0, 5.0, 7.0], dtype=torch.float32).unsqueeze(1)

# 单层感知机模型
class SingleLayerPerceptron(nn.Module):
    def __init__(self):
        super(SingleLayerPerceptron, self).__init__()
        self.fc = nn.Linear(1, 1)

    def forward(self, x):
        return self.fc(x)

model_single = SingleLayerPerceptron()
model_single.load_state_dict(torch.load('perceptron_model.path'))
model_single.eval()

with torch.no_grad():
    x_test = torch.tensor([10.0]).unsqueeze(0).unsqueeze(1)
    y_test = model_single(x_test)
    print("perceptron model structure:")
    print(model_single)
    print("test input:", x_test.item())
    print("text output:", y_test.item())

# 多层感知机模型
class MultiLayerPerceptron(nn.Module):
    def __init__(self):
        super(MultiLayerPerceptron, self).__init__()
        self.fc1 = nn.Linear(1, 64)
        self.fc2 = nn.Linear(64, 1)

    def forward(self, x):
        x = torch.relu(self.fc1(x))
        x = self.fc2(x)
        return x

model_multi = MultiLayerPerceptron()
model_multi.load_state_dict(torch.load('net_model.path'))
model_multi.eval()

with torch.no_grad():
    x_test = torch.tensor([10.0]).unsqueeze(0).unsqueeze(1)
    y_test = model_multi(x_test)
    print("net model structure:")
    print(model_multi)
    print("test input:", x_test.item())
    print("test output:", y_test.item())